#include "SocketClient.h"
#include <QTimer>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

///
/// 构造函数，初始化socket对象，连接状态，关联计时器
///
SocketClient::SocketClient()
{
    m_dataSocket  = NULL;

    m_isConnecting = true;
    isWritten = false;
    isNotParseWritten = false;
    isWeihuWritten = false;

    connect(&m_diagnoseTimer, SIGNAL(timeout()),
            this, SLOT(socketTimeOut()));
}

///
/// \brief SocketClient::isConnected
/// 判断当前对象是否正在连接，返回bool类型
/// \return
///
bool SocketClient::isConnected()
{
    if(m_dataSocket == NULL || (m_dataSocket !=  NULL &&
                                m_dataSocket->state() !=  QTcpSocket::ConnectedState))
    {
        return false;
    }
    else
    {
        return true;
    }
}

///
/// \brief SocketClient::connectTcpServer
/// 建立socket连接方法
/// \param ip地址
/// \param port号
///
void SocketClient::connectTcpServer(QHostAddress ip, int port)
{
    QTcpSocket *socket = new QTcpSocket(this);
    m_tcpServerIP = ip;
    m_tcpServerPort = port;
    socket->connectToHost(m_tcpServerIP, m_tcpServerPort);
    //连接建立等待多少时间
    socket->waitForConnected(50);
    if(socket->state() == QTcpSocket::ConnectedState)
    {
        if(m_dataSocket != NULL)
        {
            m_dataSocket->abort();
            delete m_dataSocket;
        }

        m_dataSocket = socket;
        connect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(OnReadyRead()));
        connect(m_dataSocket, SIGNAL(disconnected()), this, SLOT(OnDisconnected()));
        //发送连接成功信号
        emit socketConnected(true);
    }
    else
    {
        delete socket;
        //发送连接失败信号
        emit socketConnected(false);
    }
}
///
/// \brief SocketClient::OnReadyRead
/// 监听回传数据并实时调用到解析数据方法
///
void SocketClient::OnReadyRead()
{
    QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());

    if(socket == m_dataSocket )
    {
        m_dataPacket.data += socket->readAll();
        m_dataPacket.data = m_dataPacket.data.toHex().toUpper();

        notParseZhiling = m_dataPacket.data;
        isNotParseWritten = true;
        // 循环解析包数据，m_dataPacket.data 中可能不只一包数据
        bool isOk = false;
        do{

            isOk = parsePacket(socket, &m_dataPacket);
            //            QString tempS1 = m_dataPacket.data;
            //            jiexiZhiLing = "接收数据内容 ：" + tempS1;
        }while(isOk);
    }
    else
    {
        qDebug("socket connection abnormal");
    }


}

///
/// \brief SocketClient::parsePacket
/// \param socket对象
/// \param packet对象
/// \return
///
bool SocketClient::parsePacket(QTcpSocket *socket, SocketPacket *packet)
{


    int pIndexStart = packet->data.indexOf(NET_PACKET_START);


    if(pIndexStart < 0)
    {
        return false;
    }

    packet->data = packet->data.mid(pIndexStart); //截取从包头index_start到末尾的数据
    SocketPacket tmpPacket;
    SocketPacket recvPacket;
    tmpPacket.data = packet->data;
    recvPacket.data = packet->data;
    qDebug() << "接收的数据:"<<tmpPacket.data ;
    tmpPacket.data.remove(0, QByteArray(NET_PACKET_START).size());//删除包头

    //解析出数据类型
    if(tmpPacket.data.count() < NET_PACKET_TYPE_BYTES)
    {
        qDebug() << "数据长度有问题";
        QString tempS = recvPacket.data;
        //        qpcrLog->write("错误数据内容 ：" + tempS);
        zhiLingString = "错误数据内容 ：" + tempS;
        isWritten = true;
        return false;
    }
    QByteArray dataType = tmpPacket.data.left(NET_PACKET_TYPE_BYTES);
    tmpPacket.dataType = dataType;

    tmpPacket.data.remove(0, NET_PACKET_TYPE_BYTES);//删除数据类型

    qDebug() << tmpPacket.data << "数据包<<<<<<<<<<<<!!!!!!!!!!";


    //解析包长度
    if(tmpPacket.data.count() < NET_PACKET_LTNGTH_BYTES)
    {

        qDebug("数据长度的字节数有问题");
        QString tempS = recvPacket.data;
        //        qpcrLog->write("错误数据内容 ：" + tempS);
        zhiLingString = "错误数据内容 ：" + tempS;
        isWritten = true;
        return false;
    }
    bool isOk;
    QString len = tmpPacket.data.mid(0, NET_PACKET_LTNGTH_BYTES);
    int num = tmpPacket.data.mid(0, NET_PACKET_LTNGTH_BYTES).toInt(&isOk,16);
    tmpPacket.length = num ;
    if(isOk == false)
    {
        packet->data.remove(0, QByteArray(NET_PACKET_START).size());//删除包头
        if(packet->data.indexOf(NET_PACKET_START) >= 0)
        {

            return true;//有可能出现粘包的情况，继续解析后面数据
        }
        else
        {
            QString tempS = recvPacket.data;
            //            qpcrLog->write("错误数据内容 ：" + tempS);
            zhiLingString = "错误数据内容 ：" + tempS;
            isWritten = true;
            return false;
        }
    }

    //数据到达包长度()
    tmpPacket.data.remove(0, NET_PACKET_LTNGTH_BYTES);//删除数据长度
    if(tmpPacket.length > tmpPacket.data.count() + 4)
    {
        qDebug() << "好好看看数据长度问题";
        QString tempS = recvPacket.data;
        //        qpcrLog->write("错误数据内容 ：" + tempS);
        zhiLingString = "错误数据内容 ：" + tempS;
        isWritten = true;
        return false;
    }

    //包尾是否匹配
    tmpPacket.data.resize(tmpPacket.length * 2);//删除多余数据
    if(tmpPacket.data.endsWith(NET_PACKET_END_READ) == false)
    {

        packet->data.remove(0, QByteArray(NET_PACKET_START).size());//删除包头
        if(packet->data.indexOf(NET_PACKET_START) >= 0)
        {

            return true;//有可能出现粘包的情况，继续解析后面数据
        }
        else
        {
            qDebug() << "包尾不匹配，看看是不是AA55";
            QString tempS = recvPacket.data;
            //            qpcrLog->write("错误数据内容 ：" + tempS);
            zhiLingString = "错误数据内容 ：" + tempS;
            isWritten = true;
            return false;
        }
    }

    qint16 numEnd = QByteArray(NET_PACKET_END_READ).count();
    tmpPacket.data.resize((tmpPacket.length*2) - numEnd); //删除包尾
    //更新包的长度
    //tmpPacket.length -= n;
    qDebug() << tmpPacket.length << "数据包长度<<<<<<<<<<<<!!!!!!!!!!";
    //发送数据包信号
    if(socket == m_dataSocket)
    {
        //对实验类型的初步判断
        if (tmpPacket.dataType == QByteArray(NET_PACKET_TYPE_SHIYAN)){
            //温度回传的长度固定为18个字节
            if (tmpPacket.length == 18) {
                //防止故障信息到达18个字节，影响判断
                if(tmpPacket.data.right(4).contains("0000")||tmpPacket.data.right(4).contains("0001")
                        ||tmpPacket.data.right(4).contains("0002")||tmpPacket.data.right(4).contains("0003")
                        ||tmpPacket.data.right(4).contains("0004")||tmpPacket.data.right(4).contains("0005")
                        ||tmpPacket.data.right(4).contains("0006")||tmpPacket.data.right(4).contains("0007")
                        ||tmpPacket.data.right(4).contains("0008")){
                    //发送温度解析信号
                    emit dataWenduPacketReady(tmpPacket);
                    QString tempS = tmpPacket.data;
                    //                    qpcrLog->write("已接收温度回传数据内容 ：" + tempS);
                    zhiLingString = "已接收温度回传数据内容 ：" + tempS;
                    isWritten = true;
                }else{
                    //发送自检信息信号
                    emit dataZijianPacketReady(tmpPacket);

                }
            }
            //等于338肯定是实验恢复荧光回传信息了
            else if(tmpPacket.length == 330){
                emit SocketClient::dataShiyanYingguangPacketReady(tmpPacket);
                QString tempS = tmpPacket.data;
            }
            else if(tmpPacket.length == 10){
//                emit SocketClient::dataShiyanYingguangPacketReady(tmpPacket);
                emit dataZijianPacketReady(tmpPacket);
                QString tempS = tmpPacket.data;
                zhiLingString = "已接收实验指令数据内容 ：" + tempS;
                isWritten = true;
            }
            //大于300个字节肯定是荧光回传信息了
            else if(tmpPacket.length > 300){
                emit dataYingguangPacketReady(tmpPacket);
                QString tempS = tmpPacket.data;
                //                qpcrLog->write("已接收荧光数据内容 ：" + tempS);
                zhiLingString = "已接收荧光数据内容 ：" + tempS;
                isWritten = true;

            }
            //上述情况都不是，就是自检信息
            else
            {
                emit dataZijianPacketReady(tmpPacket);
                QString tempS = tmpPacket.data;
                zhiLingString = "已接收实验指令数据内容 ：" + tempS;
                isWritten = true;
            }

        }
        //扩增回传
        if (tmpPacket.dataType == QByteArray(NET_PACKET_TYPE_KUOZENG)){
            emit dataKuozengPacketReady(tmpPacket);
            QString tempS = tmpPacket.data;
            //            qpcrLog->write("已接收扩增回传数据内容 ：" + tempS);
            zhiLingString = "已接收扩增回传数据内容 ：" + tempS;
            isWritten = true;
        }
        //样本回传
        if (tmpPacket.dataType == QByteArray(NET_PACKET_TYPE_YANGBEN)){
            emit dataYangbenPacketReady(tmpPacket);
            QString tempS = tmpPacket.data;
            //            qpcrLog->write("已接收样本回传数据内容 ：" + tempS);
            zhiLingString = "已接收样本回传数据内容 ：" + tempS;
            isWritten = true;
        }
        if (tmpPacket.dataType == QByteArray(NET_PACKET_TYPE_WEIHU)){
            QString tempS = tmpPacket.data;
            weihuZhiling = "已接收维护回传数据内容 ：" + tempS;
            isWeihuWritten = true;

            emit dataWeihuPacketReady(tmpPacket);
        }
        if (tmpPacket.dataType == QByteArray(NET_PACKET_TYPE_IP)){
            emit dataIPPacketReady(tmpPacket);
        }
    }

    //删除当前包数据
    packet->data.remove(0,
                        QByteArray(NET_PACKET_START).size()
                        + NET_PACKET_LTNGTH_BYTES
                        + tmpPacket.length);

    return true;
}

///
/// \brief SocketClient::send
/// 发送方法封装
/// \param data 数据区
/// \param dataType 数据类型
///
void SocketClient::send(QByteArray data, QByteArray dataType)
{
    QTcpSocket *socket = m_dataSocket;

    if(socket == NULL || (socket !=  NULL &&
                          socket->state() !=  QTcpSocket::ConnectedState))
    {
        return;
    }

    QByteArray packet =  data ;
    //长度占2字节，前面补零
    int size = (packet.size() + 4)/2;

    QByteArray length = QByteArray::number(size,16).toUpper();
    length = QByteArray(NET_PACKET_LTNGTH_BYTES, '0') + length;
    //.right方法访问数组后两个字节
    length = length.right(NET_PACKET_LTNGTH_BYTES);
    packet.insert(0, NET_PACKET_START + dataType + length);//插入 [包头 + 数据长度]
    packet+= NET_PACKET_END_SEND;
    QString sendPacket = packet;
    QByteArray buf = GetHexValue(sendPacket);
    socket->write(buf);
}


QString SocketClient::sendWord(QByteArray data, QByteArray dataType)
{

    QByteArray packet =  data ;
    //长度占2字节，前面补零
    int size = (packet.size() + 4)/2;

    QByteArray length = QByteArray::number(size,16).toUpper();
    length = QByteArray(NET_PACKET_LTNGTH_BYTES, '0') + length;
    //.right方法访问数组后两个字节
    length = length.right(NET_PACKET_LTNGTH_BYTES);
    packet.insert(0, NET_PACKET_START + dataType + length);//插入 [包头 + 数据长度]
    packet+= NET_PACKET_END_SEND;
    QString sendPacket = packet;
    qDebug() << packet;
    return sendPacket;
}

///
/// \brief SocketClient::socketTimeOut
/// 超时方法
///
void SocketClient::socketTimeOut()
{
    qDebug("socketTimeOut !");
    m_diagnoseTimer.stop();
    OnDisconnected();
}
///
/// \brief SocketClient::closeSocket
/// 关闭连接方法，并释放内存
///
void SocketClient::closeSocket()
{
    if(m_dataSocket != NULL)
    {
        m_dataSocket->abort();
        delete m_dataSocket;
    }
    else{
        qDebug()<<"未建立连接";
    }
}

///
/// \brief SocketClient::getsendData
/// 拼接三个字符串
/// \param deviceID
/// \param shiyanID
/// \param cmd
/// \return 直接返回QByteArray
///
QByteArray SocketClient::getsendData(QString deviceID, QString shiyanID, QString cmd)
{

    QString getsendData = deviceID + shiyanID + cmd;
    return getsendData.toLatin1();
}

///
/// \brief SocketClient::getsendData
/// 拼接四个字符串
/// \param deviceID
/// \param shiyanID
/// \param cmd
/// \param wendu
/// \return 直接返回QByteArray
///
QByteArray SocketClient::getsendData(QString deviceID, QString shiyanID, QString cmd, QString wendu)
{

    QString getsendData = deviceID + shiyanID + cmd + wendu;
    return getsendData.toLatin1();
}

///
/// \brief SocketClient::getsendData
/// 拼接两个字符串
/// \param shiyanID
/// \param cmd
/// \return 直接返回QByteArray
///
QByteArray SocketClient::getsendData(QString shiyanID, QString cmd)
{
    QString getsendData = shiyanID + cmd;
    return getsendData.toLatin1();
}

QByteArray SocketClient::getsendData(QString deviceID, QString shiyanID, QString cmd, QString wendu, QString str5)
{

    QString getsendData = deviceID + shiyanID + cmd + wendu + str5;
    return getsendData.toLatin1();
}
QByteArray SocketClient::getsendData(QString str1, QString str2, QString str3, QString str4, QString str5, QString str6, QString str7)
{

    QString getsendData = str1 + str2 + str3 + str4 + str5 + str6 +str7;
    return getsendData.toLatin1();
}
///
/// \brief SocketClient::parsedataPacket
/// 解析设备状态方法
/// \param recvdata
/// \return 返回字符串数据
///
QStringList SocketClient::parsedataPacket(QString recvdata)
{
    QStringList list;

    QString deviceID = recvdata.mid(0,4);
    list.append(deviceID);
    recvdata = recvdata.remove(0,4);

    QString shiyanID = recvdata.mid(0,4);
    list.append(shiyanID);
    recvdata = recvdata.remove(0,4);

    QString status = recvdata.mid(0,4);
    if(status.contains("0000",Qt::CaseSensitive)){
        QString b = "接收成功";
        list.append(b);
        recvdata = recvdata.remove(0,4);
    }
    else if(status.contains("0001",Qt::CaseSensitive)){
        QString b = "设备正在运行";
        list.append(b);
        recvdata = recvdata.remove(0,4);
    }
    //    else if(status.contains("0002",Qt::CaseSensitive)){
    //        QString b = "设备正在运行";
    //        list.append(b);
    //        recvdata = recvdata.remove(0,4);
    //    }
    else{
        QString msg = recvdata;
        list.append(msg);
        cfs = new ConfigFileService();
        QHash<QString,QString> commandAndFault = cfs->getCommandAndFault();
        QList<QString> command = commandAndFault.keys();
        for(int i = 0;i < command.size();i ++){
            if(msg.contains(command[i])) list.append(commandAndFault[command[i]]);
        }
    }
    return list;
}
///
/// \brief SocketClient::parseYingguangdataPacket
/// 解析荧光数据方法
/// \param socketData
/// \return
///
QVector<QStringList> SocketClient::parseYingguangdataPacket(SocketPacket socketData)
{
    QString recvdata = socketData.data;

    QStringList returnlist; //返回的字符串列表
    QStringList kongweilist;
    QStringList chaifen;

    QVector<QStringList> addlist;
    int number;//202C分割后的长度

    //取出实验ID
    QString shiyanID = recvdata.mid(0,4);
    //实验ID加入list
    returnlist.append(shiyanID);
    //移除实验ID
    recvdata = recvdata.remove(0,4);
    addlist.append(returnlist);
    if(recvdata.endsWith("203B")){
        //逗号分割
        QStringList recvlist = recvdata.split("202C");
        number = recvlist.count();
        for(int i=0;i<number;i++){
            chaifen = analyYingguangData(recvlist[i]);
            QStringList content = chaifen;
            kongweilist = analyKongweiRowData(content[4]);
            content.removeAt(4);
            addlist.append(content);
            addlist.append(kongweilist);
        }
    }
    else{
        qDebug()<< "请检测荧光数据";
    }
    qDebug()<<"~~~~~~~~~~~~"<<addlist;
    return addlist;
}


///
/// \brief SocketClient::analysisData
/// 分析设备状态
/// \param socketData
/// \return
///
QStringList SocketClient::analysisData(SocketPacket socketData){
    QString analydata = socketData.data;
    qDebug() << "analydata111" << analydata;
    QStringList returnlist;
    QStringList recvlist = parsedataPacket(analydata);
    QString deviceID = recvlist[0];
    QString shiyanID = recvlist[1];
    QString status = recvlist[2];
    recvlist.removeAt(0);
    recvlist.removeAt(0);
    if(status.contains("0")){
        recvlist.removeAt(0);
    }

    qDebug() << "recvlist" << recvlist;

    if(recvlist.size() != 0){
        QString show = recvlist.join(",");
        returnlist.append(deviceID);
        returnlist.append(shiyanID);
        returnlist.append(show);
        return returnlist;
    }
    else{
        QString show = "接收到的数据异常，解析不成功";
        returnlist.append(deviceID);
        returnlist.append(shiyanID);
        returnlist.append(show);
        return returnlist;
    }
}

//取维护信息的回调函数
QStringList SocketClient::parseWeihu(QString analydata){
    QStringList returnlist;
    //取出维护项及命令字
    QString type = analydata.mid(0,4);
    analydata = analydata.remove(0,4);
    returnlist.append(type);
    //取出参数1
    QString param1 = analydata.mid(0,4);
    analydata = analydata.remove(0,4);
    returnlist.append(param1);
    //取出参数2
    QString param2 = analydata.mid(0,4);
    analydata = analydata.remove(0,4);
    returnlist.append(param2);
    //取出参数3
    QString param3 = analydata.mid(0,4);
    analydata = analydata.remove(0,4);
    returnlist.append(param3);
    //取出参数4
    QString param4 = analydata.mid(0,4);
    analydata = analydata.remove(0,4);
    returnlist.append(param4);
    //取出参数5
    QString param5 = analydata.mid(0,4);
    analydata = analydata.remove(0,4);
    returnlist.append(param5);
    return returnlist;
}


///
/// \brief SocketClient::analyWeihudataPacket
/// 解析维护信息
/// \param socketData
/// \return
///
QStringList SocketClient::analyWeihudataPacket(SocketPacket socketData){
    QString analydata = socketData.data;
    QStringList analylist = parseWeihu(analydata);
    QStringList returnlist;
    QString stuats = "";
    QString type = analylist[0];

    if(type.contains("80")){
        stuats = "加热丝维护数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
    }

    if(type.contains("81")){
        stuats = "传感器温度维护数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
    }

    if(type.contains("82")){
        stuats = "扰流风扇数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
    }

    if(type.contains("83")){
        stuats = "扰流风扇开关数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
    }

    if(type.contains("84")){
        stuats = "排风风扇数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
    }

    if(type.contains("85")){
        stuats = "对流风扇数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
    }

    if(type.contains("86")){
        stuats = "标准温度传感器数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
    }

    if(type.contains("87")){
        stuats = "热室原点位置校准数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
    }

    if(type.contains("88")){
        stuats = "孔位偏差校正数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
        returnlist.append(analylist[4]);
    }

    if(type.contains("89")){
        stuats = "通道开关数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
    }

    if(type.contains("8A")){
        stuats = "通道值数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
    }

    if(type.contains("8B")){
        stuats = "激发强度数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
    }

    if(type.contains("8C")){
        stuats = "荧光增益数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
    }

    if(type.contains("8D")){
        stuats = "门锁数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
    }

    if(type.contains("8E")){
        stuats = "闭盖检测数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
    }

    if(type.contains("8F")){
        stuats = "门锁校正数据传输成功";
        returnlist.append(stuats);
        returnlist.append(analylist[1]);
        returnlist.append(analylist[2]);
        returnlist.append(analylist[3]);
    }
    return returnlist;
}

///
/// \brief SocketClient::analyKuozengData
/// 解析扩增数据
/// \param socketData
/// \return
///
QStringList SocketClient::analyKuozengData(SocketPacket socketData){
    return analysisData(socketData);

}

//解析ip返回数据
QString SocketClient::analyIPdataPacket(SocketPacket socketData){
    QString status = "";
    QString analydata = socketData.data;
    if(analydata!=""){
        status = "IP和端口数据传输成功";
    }
    return  status;
}

//解析温度返回数据
QStringList SocketClient::analyWenduData(SocketPacket socketData){
    QString recvdata = socketData.data;
    QStringList recvlist;

    QString deviceID = recvdata.mid(0,4);
    recvlist.append(deviceID);
    recvdata = recvdata.remove(0,4);

    QString shiyanID = recvdata.mid(0,4);
    recvlist.append(shiyanID);
    recvdata = recvdata.remove(0,4);

    QString stage = recvdata.mid(0,4);
    recvlist.append(stage);
    recvdata = recvdata.remove(0,4);

    QString No= recvdata.mid(0,4);
    recvlist.append(No);
    recvdata = recvdata.remove(0,4);

    QString step= recvdata.mid(0,4);
    recvlist.append(step);
    recvdata = recvdata.remove(0,4);

    QString xunhuanshu = recvdata.mid(0,4);
    recvlist.append(xunhuanshu);
    recvdata = recvdata.remove(0,4);

    QString wendu = recvdata.mid(0,4);
    recvlist.append(wendu);
    recvdata = recvdata.remove(0,4);

    QString msg = recvdata;
    recvlist.append(msg);

    return recvlist;
}
///
/// \brief SocketClient::analyYangbenData
/// 解析样本数据返回状态
/// \param socketData
/// \return
///
QStringList SocketClient::analyYangbenData(SocketPacket socketData){
    return  analysisData(socketData);
}

///
/// \brief SocketClient::analyYingguangData
/// 取荧光数据的方法
/// \param yingguangmsg
/// \return
///
QStringList SocketClient::analyYingguangData(QString yingguangmsg){
    QStringList list;
    //取出阶段
    QString stage = yingguangmsg.mid(0,4);
    list.append(stage);
    yingguangmsg = yingguangmsg.remove(0,4);

    //取出编号
    QString num = yingguangmsg.mid(0,4);
    list.append(num);
    yingguangmsg = yingguangmsg.remove(0,4);

    //取出步骤
    QString step = yingguangmsg.mid(0,4);
    list.append(step);
    yingguangmsg = yingguangmsg.remove(0,4);

    //取出循环数
    QString xunhuanshu = yingguangmsg.mid(0,4);
    list.append(xunhuanshu);
    yingguangmsg = yingguangmsg.remove(0,4);


    QString kongweimsg = yingguangmsg;
    list.append(kongweimsg);
    return list;
}
///
/// \brief SocketClient::analyKongweiRowData
/// 取荧光中每个孔位的方法
/// \param kongweimsg
/// \return
///
QStringList SocketClient::analyKongweiRowData(QString kongweimsg){
    QStringList list;
    for(int i= 0 ; i<32; i++){
        QString position = kongweimsg.mid(0,20);
        list.append(position);
        kongweimsg = kongweimsg.remove(0,20);
    }

    return list;
}


//将所有的孔位加上四个通道的值都存到list链表里面
QStringList SocketClient::analyYingguangHuifuData(QString yingguangmsg){
    QStringList list;
    //最后是空位值
    QString kongweimsg = yingguangmsg;
    list.append(kongweimsg);
    return list;
}


///
/// \brief SocketClient::parseYingguangdataPacket
/// 解析实验恢复荧光数据方法
/// \param socketData
/// \return
///
QVector<QStringList> SocketClient::parseShiyanYingguangdataHuifuPacket(SocketPacket socketData)
{
    QString recvdata = socketData.data;
        qDebug() << "recvdata" << recvdata;
        QStringList returnlist; //返回的字符串列表
        QStringList kongweilist;
        QStringList chaifen;

        QVector<QStringList> addlist;
        //取出设备ID
        QString deviceID = recvdata.mid(0,4);
        //设备ID加入list
        returnlist.append(deviceID);
        //移除设备ID
        recvdata = recvdata.remove(0,4);

        //取出实验ID
        QString shiyanID = recvdata.mid(0,4);
        //实验ID加入list
        returnlist.append(shiyanID);
        //移除实验ID
        recvdata = recvdata.remove(0,4);

        //取出循环数
        QString xunhuanID = recvdata.mid(0,4);
        //实验ID加入list
        returnlist.append(xunhuanID);
        //移除循环ID
        recvdata = recvdata.remove(0,4);



        //将荧光数据之前的信息拿出来
        addlist.append(returnlist);
        chaifen = analyYingguangHuifuData(recvdata);

        QStringList content = chaifen;

        kongweilist = analyKongweiRowData(content[0]);

        //这里是真正的荧光数据
        addlist.append(kongweilist);
        return addlist;
}


//发送自检命令
void SocketClient::sendZijianCmd(QString deviceID)
{
    QByteArray buf = getsendData(deviceID, "0000", NET_CMD_ZIJIAN);
    send(buf, NET_PACKET_TYPE_SHIYAN);
    sendWord(buf,NET_PACKET_TYPE_SHIYAN);
}

//发送停止命令
void SocketClient::sendStopCmd(QString deviceID, QString shiyanID,QString wendu)
{
    QByteArray buf = getsendData(deviceID, shiyanID, NET_CMD_STOP,wendu);
    send(buf, NET_PACKET_TYPE_SHIYAN);
    //    qpcrLog->write("已发送暂停命令" + sendWord(buf,NET_PACKET_TYPE_SHIYAN));
    zhiLingString = "已发送暂停命令" + sendWord(buf,NET_PACKET_TYPE_SHIYAN);
    isWritten = true;
}

//发送继续命令
void SocketClient::sendContiueCmd(QString deviceID, QString shiyanID)
{
    QByteArray buf = getsendData(deviceID, shiyanID, NET_CMD_CONTINUE,"0000");
    send(buf, NET_PACKET_TYPE_SHIYAN);
    //    qpcrLog->write("已发送继续命令" + sendWord(buf,NET_PACKET_TYPE_SHIYAN));
    zhiLingString = "已发送继续命令" + sendWord(buf,NET_PACKET_TYPE_SHIYAN);
    isWritten = true;
}

//发送关闭命令
void SocketClient::sendCloseCmd(QString deviceID, QString shiyanID)
{
    QByteArray buf = getsendData(deviceID, shiyanID, NET_CMD_CLOSE,"0000");
    send(buf, NET_PACKET_TYPE_SHIYAN);
    //    qpcrLog->write("已发送关闭命令" + sendWord(buf,NET_PACKET_TYPE_SHIYAN));
    zhiLingString = "已发送关闭命令" + sendWord(buf,NET_PACKET_TYPE_SHIYAN);
    isWritten = true;
}

//发送扩增数据
void SocketClient::sendKuozengData(QString shiyanID,QString content)
{
    QByteArray buf = getsendData(shiyanID, content);
    qpcrLog = new QpcrLog(shiyanID);
    qpcrLog->init();
    send(buf, NET_PACKET_TYPE_KUOZENG);
    qDebug() << sendWord(buf, NET_PACKET_TYPE_KUOZENG);
    //    qpcrLog->write("已发送扩增数据 ：" + sendWord(buf, NET_PACKET_TYPE_KUOZENG));
    zhiLingString = "已发送扩增数据" + sendWord(buf,NET_PACKET_TYPE_KUOZENG);
    isWritten = true;
}

//发送样本数据
void SocketClient::sendYangbenData(QString shiyanID,QString content)
{
    QByteArray buf = getsendData(shiyanID, content);
    send(buf, NET_PACKET_TYPE_YANGBEN);
    //    qpcrLog->write("已发送样本数据 ：" + sendWord(buf, NET_PACKET_TYPE_YANGBEN));
    zhiLingString = "已发送样本数据" + sendWord(buf,NET_PACKET_TYPE_YANGBEN);
    isWritten = true;
}

//发送荧光请求数据
void SocketClient::sendYingguangData(QString deviceID, QString shiyanID)
{
    QByteArray buf = getsendData(deviceID, shiyanID, NET_CMD_YINGGUANG);
    qDebug() << "荧光指令" << buf;
    send(buf, NET_PACKET_TYPE_SHIYAN);
    //    qpcrLog->write("已发送请求荧光命令" + sendWord(buf,NET_PACKET_TYPE_SHIYAN));
    zhiLingString = "已发送请求荧光命令" + sendWord(buf,NET_PACKET_TYPE_SHIYAN);
    isWritten = true;
}

//发送温度请求数据
void SocketClient::sendWenduData(QString deviceID, QString shiyanID)
{

    QByteArray buf = getsendData(deviceID, shiyanID,NET_CMD_WENDU);
    send(buf, NET_PACKET_TYPE_SHIYAN);
    //    qpcrLog->write("已发送请求温度命令" + sendWord(buf,NET_PACKET_TYPE_SHIYAN));
    zhiLingString = "已发送请求温度命令" + sendWord(buf,NET_PACKET_TYPE_SHIYAN);
    isWritten = true;


}




//发送实验恢复请求数据
void SocketClient::sendShiyanHuifuData(QString deviceID, QString shiyanID)
{

    QByteArray buf = getsendData(deviceID, shiyanID,"0009");
    send(buf, NET_PACKET_TYPE_SHIYAN);
    qDebug() << "已发送实验恢复命令" << sendWord(buf, NET_PACKET_TYPE_SHIYAN);

    //    qpcrLog->write("已发送请求温度命令" + sendWord(buf,NET_PACKET_TYPE_SHIYAN));
    //    zhiLingString = "已发送请求温度命令" + sendWord(buf,NET_PACKET_TYPE_SHIYAN);
    //    isWritten = true;


}
//发送故障请求数据
void SocketClient::sendGuzhangData(QString deviceID, QString shiyanID)
{
    QByteArray buf = getsendData(deviceID, shiyanID, NET_CMD_GUZHANG);
    send(buf, NET_PACKET_TYPE_SHIYAN);
    //    qpcrLog->write("已发送请求故障信息命令" + sendWord(buf,NET_PACKET_TYPE_SHIYAN));
    zhiLingString = "已发送请求故障信息命令" + sendWord(buf,NET_PACKET_TYPE_SHIYAN);
    isWritten = true;
}

/*------------------------------
 * 维护发送方法
 ------------------------------*/
/*
加热丝维护
*/
void SocketClient::sendWeihuJiaresi(QString zhanKongbi, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_JIARESI, zhanKongbi, NET_WEIHU_KONG, NET_WEIHU_KONG,
                                 NET_WEIHU_KONG, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求加热丝维护信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;

}
/*
传感器温度维护
*/
void SocketClient::sendWeihuSersorwendu(QString wendu1, QString wendu2, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_SERSORWENDU, wendu1, wendu2, NET_WEIHU_KONG,
                                 NET_WEIHU_KONG, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求传感器温度维护信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;
}
/*
扰流风扇维护
*/
void SocketClient::sendWeihuRaoluanfeng(QString zhanKongbi, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_RAOLUANFENGSHAN, zhanKongbi, NET_WEIHU_KONG, NET_WEIHU_KONG,
                                 NET_WEIHU_KONG, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求扰流风扇维护信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;
}
/*
扰流风扇开关维护
*/
void SocketClient::sendWeihuRaoluanfengshanKaiguan(QString Kaiguan, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_RAOLUANFENGSHANKAIGUAN, Kaiguan, NET_WEIHU_KONG, NET_WEIHU_KONG,
                                 NET_WEIHU_KONG, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求扰流风扇开关维护信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;
}
/*
排风风扇维护
*/
void SocketClient::sendWeihuPaifengfengshan(QString Kaiguan, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_PAIFENGFENGSHAN, Kaiguan, NET_WEIHU_KONG, NET_WEIHU_KONG,
                                 NET_WEIHU_KONG, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求排风风扇维护信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;
}
/*
对流风扇维护
*/
void SocketClient::sendWeihuDuiliufengshan(QString Zhankongbi, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_DUILIUFENGSHAN, Zhankongbi, NET_WEIHU_KONG, NET_WEIHU_KONG,
                                 NET_WEIHU_KONG, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求对流风扇维护信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;
}
/*
标准温度传感器校准维护
*/
void SocketClient::sendWeihuBiaozhunWendu(QString wendu, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_BIAOZHUNWENDU, wendu, NET_WEIHU_KONG, NET_WEIHU_KONG,
                                 NET_WEIHU_KONG, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求标准温度传感器校准维护信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;
}
/*
热室原点位置维护
*/
void SocketClient::sendWeihuReshiyuandian(QString tongdao, QString step, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_RESHIYUANDIAN, tongdao, step, NET_WEIHU_KONG,
                                 NET_WEIHU_KONG, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求热室原点位置维护信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;
}
/*
孔位偏差校正
*/
void SocketClient::sendWeihuKongweipiancha(QString haocaitype, QString kongwei, QString yuandianpiancha,
                                           QString pingjunpiancha, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_XUANZHUANDIANJI, haocaitype, kongwei, yuandianpiancha,
                                 pingjunpiancha, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求孔位偏差校正维护信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;
}
/*
通道开关
*/
void SocketClient::sendWeihuTongdaoKaiguan(QString tongdao, QString kaiguan, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_TONGDAOKAIGUAN, tongdao, kaiguan, NET_WEIHU_KONG,
                                 NET_WEIHU_KONG, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求通道开关信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;
}
/*
通道值
*/
void SocketClient::sendWeihuTongdaozhi(QString tongdao, QString Yingguangzhi, QString AD, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_TONGDAOZHI, tongdao, Yingguangzhi, AD,
                                 NET_WEIHU_KONG, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求通道值信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;
}
/*
激发增强
*/
void SocketClient::sendWeihuJifaqiangdu(QString tongdao,QString qiangdu, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_JIFAQIANGDU, tongdao, qiangdu, NET_WEIHU_KONG,
                                 NET_WEIHU_KONG, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求激发增强信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;
}
/*
荧光增益
*/
void SocketClient::sendWeihuYingguangzengyi(QString tongdao, QString zengyi, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_YINGGUANGZENGYI, tongdao, zengyi, NET_WEIHU_KONG,
                                 NET_WEIHU_KONG, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求荧光增益信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;
}
/*
门锁
*/
void SocketClient::sendWeihuMensuo(QString pingbi, QString kaiguan, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_MENSUO, pingbi, kaiguan, NET_WEIHU_KONG,
                                 NET_WEIHU_KONG, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求门锁信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;
}
/*
闭盖检测
*/
void SocketClient::sendWeihuBigai(QString pingbi, QString kaiguan, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_BIGAIJIANCE, pingbi, kaiguan, NET_WEIHU_KONG,
                                 NET_WEIHU_KONG, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求闭盖检测信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;
}
/*
门锁校正
*/
void SocketClient::sendWeihuMensuojiaozheng(QString zhankongbi, QString cmd){
    QByteArray buf = getsendData(NET_WEIHU_BIGAIJIANCE, zhankongbi, NET_WEIHU_KONG, NET_WEIHU_KONG,
                                 NET_WEIHU_KONG, NET_WEIHU_KONG, cmd);
    send(buf, NET_PACKET_TYPE_WEIHU);

    weihuZhiling = "已发送请求门锁校正信息命令" + sendWord(buf,NET_PACKET_TYPE_WEIHU);
    isWeihuWritten = true;
}


/*
发送IP和port
*/
void SocketClient::sendIP(QString ip1, QString ip2, QString ip3, QString ip4, QString port){
    QByteArray buf = getsendData(ip1, ip2, ip3, ip4, port);
    send(buf, NET_PACKET_TYPE_IP);
}


/*------------------------------
 * 程序计算crc16-modbus
 ------------------------------*/
unsigned short SocketClient::crc16(unsigned char *CmmBuf, unsigned char Len)
{
    unsigned short crc = 0xFFFF;
    unsigned char i;
    unsigned char j;
    // if(Len > 78) return 1;
    if(Len > 200) return 1;
    j = 0;
    while(j < Len)
    {
        crc ^= CmmBuf[j];
        for(i=0; i < 8; i++)
        {
            if(crc & 0x01)
            {
                crc >>= 1;
                crc ^= 0xA001;
            }
            else
            {
                crc >>= 1;
            }
        }
        j++;
    }
    //      CmmBuf[j] = crc % 0x100; //校验和从长度算起，长度为长度减3
    //      CmmBuf[j + 1] = crc / 0x100;
    //  qDebug() <<"MeterAgent::crc16: step 3：crc16校验"<< crc << "--" <<  crc % 0x100 << "---" << crc / 0x100;

    return crc;

}

/*------------------------------
     * 检测crc是否正确
     ------------------------------*/
bool SocketClient::crc_Checking(QString message)
{
    qDebug()<<"##################";
    //将获取到底报文两个两个以空格分开
    QString strDis,temp_st;
    int data_frame_bit = 0;//数据帧到位数
    qDebug() << message.size();
    //先判断奇偶，若为奇数在校验位前加0
    if(message.size()%2!=0){
        message.insert(message.size()-4,"0");
    }
    for(int i = 0;i < message.length(); i += 2)//填加空格
    {
        temp_st = message.mid(i,2);
        strDis += temp_st;
        strDis += " ";
        data_frame_bit ++;
    }
    if( data_frame_bit < 5 )
    {
        return false;
    }


    //将获取到底字符串报文，转成QByteArray格式，并用空格分隔获取到有多少个参数i
    QStringList strlist = strDis.split(" ");

    int data_frame_bit_crc = data_frame_bit-2;
    unsigned char crc_str[data_frame_bit_crc];
    QString crc_str_accept = "";//接收到的crc校验码
    QString crc_str_calculated = "";//计算出来的crc校验码大写
    QString crc_str_calculated_low = "";//计算出来的crc校验码小写
    for(int i = 0 ; i<data_frame_bit-2 ; i++)
    {
        //将数据帧接收到到crc检验码去掉
        crc_str[i] = strlist.at(i).toInt(0,16);

    }

    //计算出crc16校验码
    int crc_reaout_int = this -> crc16(crc_str,data_frame_bit-2);
    crc_str_calculated = QString("%1").arg(crc_reaout_int, 4, 16, QChar('0')).toUpper();//窗口文件都是大写，所以计算出到字符串字母也要大写。
    crc_str_calculated_low =  QString("%1").arg(crc_reaout_int, 4, 16, QChar('0')).toLower();//小写
    //由于编号是0，1，2，3...所以重新拼接到crc校验吗应该从len-1开始，循环两次
    for(int i = data_frame_bit-1 ; i>data_frame_bit-3 ;i--)
    {
        qDebug() << i;
        //重新拼接接收到到crc16校验码
        crc_str_accept += strlist.at(i);
    }
    qDebug() << crc_str_accept << "-------" << crc_str_calculated;
    //判断校验码是否正确，正确返回true,
    if( crc_str_calculated == crc_str_accept || crc_str_calculated_low == crc_str_accept )
    {
        return true;
    }
    else
    {
        return false;
    }
}

/*------------------------------
     * 计算出crc的直
     * 将crc拼接到message后面
     ------------------------------*/
QString SocketClient::crcCalculation(QString message)
{

    /*
     * 进来现将空格去掉
     * 进来不管有没有空格，先去掉，再按找下面标准执行。
     *
     */
    message =  message.replace(" ", "");
    /*
     * 这边传过来到没有crc校验，所以总长度不要减去2
     */
    //将获取到底报文两个两个以空格分开
    QString strDis,temp_st;
    int data_frame_bit = 0;//数据帧到位数
    for(int i = 0;i < message.length(); i += 2)//填加空格
    {
        temp_st = message.mid(i,2);
        strDis += temp_st;
        strDis += " ";
        data_frame_bit ++;
    }
    if( data_frame_bit < 5 )
    {
        return "false";
    }
    //将获取到底字符串报文，转成QByteArray格式，并用空格分隔获取到有多少个参数i
    QStringList strlist = strDis.split(" ");
    int data_frame_bit_crc = data_frame_bit;
    unsigned char crc_str[data_frame_bit_crc];

    QString crc_str_calculated_low = "";//计算出来的crc校验码小写
    for(int i = 0 ; i<data_frame_bit ; i++)
    {
        //将数据帧存放数组
        crc_str[i] = strlist.at(i).toInt(0,16);
    }

    //计算出crc16校验码
    int crc_reaout_int = this -> crc16(crc_str,data_frame_bit);
    crc_str_calculated_low =  QString("%1").arg(crc_reaout_int, 4, 16, QChar('0')).toLower();//小写

    QString strDis_crc;
    for(int i = 0;i < crc_str_calculated_low.length(); i += 2)//填加空格
    {
        temp_st = crc_str_calculated_low.mid(i,2);
        strDis_crc += temp_st;
        strDis_crc += " ";
    }
    QStringList strlist_crc = strDis_crc.split(" ");
    crc_str_calculated_low = strlist_crc.at(1)+strlist_crc.at(0);
    //qDebug() << "lo hi :" << crc_str_calculated_low ;

    /*
     * 将报文和校验拼接，形成带校验到报文，别且返回。
     */
    message =  crc_str_calculated_low;

    return message;
}

QByteArray SocketClient::GetHexValue(QString str)
{
    QByteArray senddata;
    int hexdata,lowhexdata;
    int hexdatalen = 0;
    int len = str.length();
    senddata.resize(len/2);
    char lstr,hstr;
    for(int i=0; i<len; )
    {
        hstr=str[i].toLatin1();
        if(hstr == ' ')
        {
            i++;
            continue;
        }
        i++;
        if(i >= len)
            break;
        lstr = str[i].toLatin1();
        hexdata = ConvertHexChar(hstr);
        lowhexdata = ConvertHexChar(lstr);
        if((hexdata == 16) || (lowhexdata == 16))
            break;
        else
            hexdata = hexdata*16+lowhexdata;
        i++;
        senddata[hexdatalen] = (char)hexdata;
        hexdatalen++;
    }
    senddata.resize(hexdatalen);
    return senddata;
}

char SocketClient::ConvertHexChar(char ch)
{
    if((ch >= '0') && (ch <= '9'))      //限定字符0-9
        return ch-0x30;
    else if((ch >= 'A') && (ch <= 'F')) //限定字符A-F
        return ch-'A'+10;
    else if((ch >= 'a') && (ch <= 'f')) //限定字符a-f
        return ch-'a'+10;
    else return (-1);
}
void SocketClient::OnDisconnected()
{
    QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());

    if(m_dataSocket !=  NULL)
    {
        m_dataSocket->abort();
        m_dataSocket = NULL;
    }

    m_diagnoseTimer.stop();

    emit socketConnected(false);

}

/*------------------------------
     * 发送指令信息到Ui界面
     ------------------------------*/
QString SocketClient::returnZhiLing2Ui(){
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date = current_date_time.toString("yyyy/MM/dd hh:mm:ss.zzz");
    QString isConnectedState;
    QString completeZhilingString;
    isWritten = false;
    if(isConnected()) isConnectedState = "true";
    else isConnectedState = "false";
    completeZhilingString = current_date + " " + zhiLingString + "，通信状态：" + isConnectedState;
    qpcrLog->write(zhiLingString + "，通信状态：" + isConnectedState);
    return completeZhilingString;
}

QString SocketClient::returnNotParseZhiling2Ui(){
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date = current_date_time.toString("yyyy/MM/dd hh:mm:ss.zzz");
    isNotParseWritten = false;
    return current_date + " " + "接收到的未解析数据：" + notParseZhiling;
}

QString SocketClient::returnWeiHu2Ui(){
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date = current_date_time.toString("yyyy/MM/dd hh:mm:ss.zzz");
    QString isConnectedState;
    QString completeZhilingString;

    isWeihuWritten = false;

    if(isConnected()) isConnectedState = "true";
    else isConnectedState = "false";

    completeZhilingString = current_date + " " + weihuZhiling + "，通信状态：" + isConnectedState;
    return completeZhilingString;
}
